package com.second.lease.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.second.common.pojo.R;
import com.second.lease.dao.CarShopDao;
import com.second.lease.pojo.entity.CarShop;
import com.second.lease.pojo.vo.CarShopVO;
import com.second.lease.service.CarShopService;
import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDateTime;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CarShopServiceImpl implements CarShopService {
    @Resource
    private CarShopDao carShopDao;
    @Override
    public R selectCarShop(CarShopVO vo) {

        if (vo.getPageno() != null) {
            QueryWrapper queryWrapper = new QueryWrapper();
            if (StringUtils.isNotEmpty(vo.getName())){
                queryWrapper.like("name",vo.getName());
            }
            if (StringUtils.isNotEmpty(vo.getAddress())){
                queryWrapper.like("address",vo.getAddress());
            }
            PageHelper.startPage(vo.getPageno(),vo.getPagesize());
            List list = carShopDao.selectList(queryWrapper);
            PageInfo pageInfo = new PageInfo(list);
            return R.success().putListData(list).putObject(pageInfo);
        } else {
            List list = carShopDao.selectList(null);
            return R.success().putListData(list);
        }
    }

    @Override
    public R updateCarShop(CarShop carShop) {
        int i = carShopDao.updateById(carShop);
        if (i >0) {
            return R.success();
        }
        return R.error();
    }

    @Override
    public R insertCarShop(CarShop carShop) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",carShop.getName());
        CarShop c = carShopDao.selectOne(queryWrapper);
        if ( c == null) {
            carShop.setAddtime(LocalDateTime.now().toString());
            int i = carShopDao.insert(carShop);
            if (i > 0) {
                return R.success();
            }
            return R.error();
        }
        return R.error();
    }

    @Override
    public R deleteCarShop(Integer id) {

        int i = carShopDao.deleteById(id);
        if (i > 0) {
            return R.success();
        }
        return R.error();
    }

    @Override
    public R showCarShopByAreaId(String areaId) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("area_id",areaId);
        List carShopList = carShopDao.selectList(queryWrapper);
        return R.success().putListData(carShopList);
    }

    @Override
    public R queryAddressById(Integer id) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        CarShop carShop = carShopDao.selectOne(queryWrapper);
        if (carShop != null) {
            return R.success().putObject(carShop);
        }
        return R.error();
    }
}
